package flst.ipiweb.persistance.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import flst.ipiweb.persistance.interfaces.IModeDePaiementDAL;
import flst.ipiweb.transverse.IPIWebException;
import flst.ipiweb.transverse.ModeDePaiement;

public class ModeDePaiementJdbcDAL implements IModeDePaiementDAL{


	public void UpdateModeDePaiement(ModeDePaiement mdpe) throws IPIWebException {
		
		
		Connection maconnection;

		try {
			maconnection = JdbcUtils.getInstance().getConnection();
			PreparedStatement req = maconnection.prepareStatement("UPDATE MODEDEPAIEMENT SET ACCESCLIENT = ?, ACCESSUPERCLIENT = ? WHERE IDMODEDEPAIEMENT = ?;");
			req.setBoolean(1, mdpe.getAccesClient());
			req.setBoolean(2, mdpe.getAccesSuperClient());
			req.setInt(3, mdpe.getId());
			req.execute();


			req.close();
			maconnection.close();
		} catch (Exception e) {
			throw new IPIWebException(e.getMessage());
		}
	}


	public List<ModeDePaiement> getAllModeDePaiement() throws IPIWebException {

		List<ModeDePaiement> lmdpe = new ArrayList<ModeDePaiement>();
		Connection maconnection;

		try {
			maconnection = JdbcUtils.getInstance().getConnection();
			Statement req = maconnection.createStatement();
			String requeteselect = "SELECT LIBELLE, ACCESCLIENT, ACCESSUPERCLIENT, IDMODEDEPAIEMENT FROM MODEDEPAIEMENT ";
			req.execute(requeteselect);

			ResultSet rs = req.getResultSet();
			while (rs.next()) {
				
					ModeDePaiement mdpe = new ModeDePaiement();
					mdpe.setLibelle(rs.getString(1));
					mdpe.setAccesClient(rs.getBoolean(2));
					mdpe.setAccesSuperClient(rs.getBoolean(3));
					mdpe.setId(rs.getInt(4));
					lmdpe.add(mdpe);
				
			}

			req.close();
			maconnection.close();
		} catch (Exception e) {
			throw new IPIWebException(e.getMessage());
		}
		return lmdpe;
	}


	
	public ModeDePaiement getModeDePaiementById(int id) throws IPIWebException {
		
		Connection maconnection;
		ModeDePaiement mdpe = new ModeDePaiement();
		
		try {
			maconnection = JdbcUtils.getInstance().getConnection();
			PreparedStatement req = maconnection.prepareStatement("SELECT LIBELLE, ACCESCLIENT, ACCESSUPERCLIENT, IDMODEDEPAIEMENT FROM MODEDEPAIEMENT WHERE IDMODEDEPAIEMENT = ? ;");
			req.setInt(1, id);
			req.execute();

			ResultSet rs = req.getResultSet();
			while (rs.next()) {
				mdpe.setLibelle(rs.getString(1));
				mdpe.setAccesClient(rs.getBoolean(2));
				mdpe.setAccesSuperClient(rs.getBoolean(3));
				mdpe.setId(id);
			}

			req.close();
			maconnection.close();
		} 
		catch (Exception e) {
			throw new IPIWebException(e.getMessage());
		}
		return mdpe;
	}


}
